﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

import andromeda.util.visitor.IVisitable;
import andromeda.util.visitor.IVisitor;

import egallery.display.gallery.PictureDisplay;
import egallery.visitors.ClearVisitor;
import egallery.visitors.ShowVisitor;

import vegas.core.CoreObject;
import vegas.errors.IllegalArgumentError;
import vegas.errors.Warning;
import vegas.events.Delegate;

/**
 * This class load an external picture in the PictureDisplay instance.
 * @author eKameleon
 */
class egallery.visitors.LoaderVisitor extends CoreObject implements IVisitor 
{

	/**
	 * Creates a new LoaderVisitor instance.
	 * @param bAutoShow the flag to indicates if the picture is auto show when is initialize.
	 * @param url the url of the external picture.
	 */
	public function LoaderVisitor( bAutoShow:Boolean , url:String ) 
	{
		
		super();
		
		autoShow = bAutoShow ;
		
		this.url = url ;
		
	}

	/**
	 * Defined if the picture is show when is loading.
	 */
	public var autoShow:Boolean ;
	
	/**
	 * The url string representation of this object.
	 */
	public var url:String ;

	/**
	 * Load a Picture object with this current url. Visit the IVisitable object. 
	 */
	public function visit( o:IVisitable ):Void
	{
		
		if (o instanceof PictureDisplay)
		{
			
			getLogger().debug( this + " visit " + o  ) ;
		
			var view = PictureDisplay(o).view ;
			
			// clear the picture.
			PictureDisplay(o).accept( new ClearVisitor() ) ;
			
			if (url.length > 0)
			{
			
				var listener:Object = {} ;
				listener.onLoadInit = Delegate.create(this, onLoadInit, PictureDisplay(o) ) ;
				
				var target:MovieClip = view.container.createEmptyMovieClip("picture", 1) ;
				
				var loader:MovieClipLoader = new MovieClipLoader() ;
				loader.addListener(listener) ;
				
				loader.loadClip(url , target) ;
				
			}
			else
			{
				throw new Warning(this + " the picture '" + o + "' can't be loading with an empty url property.") ; 	
			}
			
		}
		else
		{
			throw new IllegalArgumentError(this + " 'visit' method failed, the argument of this method must be a PictureDisplay instance.") ;	
		}
	}

	/**
	 * Invoqued when the loading of a Picture is finished and initialized.
	 */
	public function onLoadInit( target:MovieClip , picture:PictureDisplay ):Void
	{
		
		getLogger().info (  this + " picture " + picture + " is loading and init.") ;
		
		if ( autoShow == true )
		{
			picture.accept( new ShowVisitor() ) ;
		}
		
	}

}